package com.onstar.cn.common.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

import com.securepreferences.SecurePreferences;

import java.util.HashSet;
import java.util.Set;

/**
 * SharedPreferences processing tools
 *
 * @author 7828wc
 */
public class PreferencesUtls {
    private static PreferencesUtls preferenceUtils = null;
    private SharedPreferences sharedPreference;
    public static final String PREFERENCE_NAME = "onstar_preference";
    private Context context;

    public static PreferencesUtls getInstance(Context context) {
        if (preferenceUtils == null) {
            preferenceUtils = new PreferencesUtls(context);
        }
        return preferenceUtils;
    }

    private PreferencesUtls(Context context) {
        this.context = context.getApplicationContext();
//        sharedPreference = this.context.getSharedPreferences(PREFERENCE_NAME, 0);
        sharedPreference = new SecurePreferences(this.context);
    }

    public void putString(String key, String value) {
        put(key, value);
    }

    public String getString(String key, String defaultValue) {
        return (String) get(key, defaultValue);
    }

    public void putBoolean(String key, Boolean value) {
        put(key, value);
    }

    public Boolean getBoolean(String key, Boolean defaultValue) {
        return (Boolean) get(key, defaultValue);
    }

    public void putLong(String key, long value) {
        put(key, value);
    }

    public long getLong(String key, long defaultValue) {
        return (Long) get(key, defaultValue);
    }

    public void putInt(String key, int value) {
        put(key, value);
    }

    public int getInt(String key, int defaultValue) {
        return (Integer) get(key, defaultValue);
    }

    public String getString(String key) {
        return getString(key, "");
    }

    public void putSet(String key, Set<String> values) {
        Editor editor = sharedPreference.edit();
        editor.putStringSet(key, values).commit();
    }

    public void putValueToList(String key, String value) {
        Editor editor = sharedPreference.edit();
        Set<String> set = sharedPreference.getStringSet(key, null);
        if (set == null) {
            set = new HashSet<String>();
        }
        set.add(value);
        editor.putStringSet(key, set);
        editor.commit();
    }

    public boolean containsValue(String key, String value) {
        Set<String> set = sharedPreference.getStringSet(key, null);
        if (set != null && set.size() > 0) {
            return set.contains(value);
        } else {
            return false;
        }
    }

    public void put(String key, Object value) {
        Editor editor = sharedPreference.edit();
        convertValue(key, value, editor).commit();
    }

    public Object get(String key, Object defValue) {
        //if (!CommonUtil.isNull(defValue)) {
        if (defValue.getClass().equals(String.class)) {
            return decrypt(context, sharedPreference.getString(encrypt(context, key), (String) defValue), "");
//				return sharedPreference.getString(key, (String)defValue);
        } else if (defValue.getClass().equals(Boolean.class)) {
            return sharedPreference.getBoolean(key, (Boolean) defValue);
        } else if (defValue.getClass().equals(Long.class)) {
            return sharedPreference.getLong(key, (Long) defValue);
        } else if (defValue.getClass().equals(Integer.class)) {
            return sharedPreference.getInt(key, (Integer) defValue);
        } else if (defValue.getClass().equals(Float.class)) {
            return sharedPreference.getFloat(key, (Float) defValue);
        }
        //}
        return null;
    }

    /**
     * 转换value值
     *
     * @param key
     * @param value
     * @param editor
     * @return
     */
    private Editor convertValue(String key, Object value, Editor editor) {
        if (CommonUtil.isNull(editor))
            editor = sharedPreference.edit();

        if (!CommonUtil.isNull(value)) {
            if (value.getClass().equals(String.class)) {
                editor.putString(encrypt(context, key), encrypt(context, (String) value));
            } else if (value.getClass().equals(Boolean.class)) {
                editor.putBoolean(key, (Boolean) value);
            } else if (value.getClass().equals(Long.class)) {
                editor.putLong(key, (Long) value);
            } else if (value.getClass().equals(Integer.class)) {
                editor.putInt(key, (Integer) value);
            } else if (value.getClass().equals(Float.class)) {
                editor.putFloat(key, (Float) value);
            }
        } else {
            editor.putString(encrypt(context, key), "");
        }
        return editor;
    }

    // 加密
    private static String encrypt(Context context, String str) {

        String encryptStr = null;
        String key = "*#" + new DeviceUtil(context).getDeviceID() + "$&@!#*";
        try {
            encryptStr = SimpleCrypto.encrypt(key, str);
        } catch (Exception e) {
            encryptStr = str;
        }
        return encryptStr;
    }

    // 解密
    private static String decrypt(Context context, String str, String def) {
        String key = "*#" + new DeviceUtil(context).getDeviceID() + "$&@!#*";
        if (str == null) {
            return null;
        } else if (str.equals(def)) {
            return def;
        } else {
            try {
                return SimpleCrypto.decrypt(key, str);
            } catch (Exception e) {
                return def;
            }
        }
    }


    //


}
